package com.jdjc.suject.domin.service.impl;

import com.jdjc.subject.common.enums.CategoryTypeEnum;
import com.jdjc.subject.common.enums.IsDeletedFlagEnum;
import com.jdjc.subject.infra.basic.entity.SubjectCategory;
import com.jdjc.subject.infra.basic.entity.SubjectLabel;
import com.jdjc.subject.infra.basic.entity.SubjectMapping;
import com.jdjc.subject.infra.basic.service.SubjectCategoryService;
import com.jdjc.subject.infra.basic.service.SubjectLabelService;
import com.jdjc.subject.infra.basic.service.SubjectMappingService;
import com.jdjc.suject.domin.convert.SubjectLableConverter;
import com.jdjc.suject.domin.entity.SubjectLabelBO;
import com.jdjc.suject.domin.service.SubjectLableDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectLableDomainServiceImpl implements SubjectLableDomainService {
    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;
    @Resource
    private SubjectCategoryService subjectCategoryService;

    /**
     * 新增标签
     *
     * @param subjectLabelBO
     */
    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        //将BO转换为SubjectLable
        if (log.isInfoEnabled()) {
            log.info(" SubjectLableBO：add:BO:{}", subjectLabelBO);
        }
        //接受一个 SubjectCategoryBO 类型的参数，并返回一个 SubjectCategory 类型的对象。
        SubjectLabel subjectLable = SubjectLableConverter.INSTANCE.convertBOToLable(subjectLabelBO);
        subjectLable.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int count = subjectLabelService.insert(subjectLable);
        return count > 0;
    }

    /**
     * 修改标签
     *
     * @param subjectLabelBO
     * @return
     */
    @Override
    public Boolean updateLable(SubjectLabelBO subjectLabelBO) {

        if (log.isInfoEnabled()) {
            log.info("SubjectLableBO：update:BO:{}", subjectLabelBO);
        }
        //将BO转换为SubjectLabel
        SubjectLabel subjectLabel = SubjectLableConverter.INSTANCE.convertBOToLable(subjectLabelBO);
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 逻辑删除标签
     *
     * @param subjectLabelBO
     * @return
     */
    @Override
    public Boolean deleteLable(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLableBO：deleteLable:BO:{}", subjectLabelBO);
        }
        //将BO转换为SubjectLabel
        SubjectLabel subjectLabel = SubjectLableConverter.INSTANCE.convertBOToLable(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 查询分类下标签
     *
     * @param subjectLabelBO
     * @return
     */
    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLableBO：deleteLable:BO:{}", subjectLabelBO);
        }
        //如果当前分类是一级分类则查询所有标签
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBO.getCategoryId());
        if (CategoryTypeEnum.PRIMARY.getCode()==subjectCategory.getCategoryType()) {
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(String.valueOf(subjectLabelBO.getCategoryId()));
            List<SubjectLabel> labelList=subjectLabelService.queryByCondition(subjectLabel);
            List<SubjectLabelBO> boList = SubjectLableConverter.INSTANCE.lableListToBOList(labelList);
            return boList;
        }
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
        if (CollectionUtils.isEmpty(mappingList)) {
            return Collections.emptyList();
        }
        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> labelList = subjectLabelService.batchqueryById(labelIdList);
        List<SubjectLabelBO> boList = new LinkedList<>();
        labelList.forEach(label -> {
            SubjectLabelBO bo = new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            bo.setCategoryId(categoryId);
            bo.setSortNum(label.getSortNum());
            boList.add(bo);
        });
        return boList;

    }
}
